/**
 * Copyright 2007 Igor Chaves Cananéa
 *
 * This file is part of PBSFW
 *
 * PBSFW is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * PBSFW is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef _RESOURCE_H_
#define _RESOURCE_H_

#include <list>
#include <string>
#include "defines.h"
#include "process.h"
#include "resetable.h"
using namespace std;

namespace pbsfw {
    class Resource : public Resetable {
        private:
            string name;
            uint32_t maxAmount;
            uint32_t currentAmount;
            list<Process*> waitQueue;

            bool hasResource() const {
                return this->maxAmount == 0 ||
                       this->currentAmount < this->maxAmount;
            }

        public:
            Resource(string name, uint32_t maxAmount = 0);
            Resource(const Resource &r);

            const char *getName() const {
                return this->name.c_str();
            }

            bool request(Process &p) {
                if (this->hasResource()) {
                    ++this->currentAmount;
                    return true;
                }
                else {
                    this->waitQueue.push_back(&p);
                }
                return false;
            }

            void release();

            void reset() {
                this->currentAmount = 0;
                this->waitQueue.clear();
            }
    };
};

#endif
